package com.autumn.cloud.demo.consumer.controllers;

import com.autumn.cloud.demo.common.constants.DemoServiceProviderUrlConstants;
import com.autumn.cloud.demo.common.model.UserInfo;
import com.autumn.cloud.demo.common.service.ProviderCommonService;
import com.autumn.cloud.demo.consumer.client.UserInfoFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-12-10 18:50
 **/
@RestController
@Slf4j
public class UserController {

    private final ProviderCommonService providerCommonService;
    private final DiscoveryClient discoveryClient;
    private final UserInfoFeignClient userInfoFeignClient;



    public UserController(ProviderCommonService providerCommonService,
                          DiscoveryClient discoveryClient,
                          UserInfoFeignClient userInfoFeignClient) {
        this.providerCommonService = providerCommonService;
        this.discoveryClient = discoveryClient;
        this.userInfoFeignClient = userInfoFeignClient;
    }



    /**
     * demo 服务器列表
     *
     * @return
     */
    @GetMapping(path = "server/demo/list")
    public List<ServiceInstance> demoServerList() {
        List<ServiceInstance> instances = discoveryClient.getInstances(this.providerCommonService.getDemoServiceName());
        return instances;
    }

    /**
     * 用户信息
     *
     * @return
     */
    @GetMapping(path = DemoServiceProviderUrlConstants.USER_INFO_URL)
    public UserInfo userInfo() {
        UserInfo userInfo = this.userInfoFeignClient.getUserInfo();
        if (userInfo == null) {
            return null;
        }
        userInfo.setMsg(userInfo.getMsg() + " -> ZK-Feign-client 转发");
        log.info("用户信息请求--ZK-Feign-client");
        return userInfo;
    }

}